Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

238
Visualizações
Why does document.getElementsByClassName("className")[0].style.display return an empty string

I'm trying to create a button that makes an element disappear/reappear depending on whether the display property is equal "block" or "none" respectively. Here's what I tried:

// this is a function that executes when the button is pressed
{
  let a = document.getElementsByClassName("photo");
  // here a[0] returns the <img> that i want to delete/undelete but  
  // a[0].style.display returns an empty string
  if(a[0].style.display == "block") a[0].style.display = "none"; // so this doesnt work
  else if(a[0].style.display == "none") a[0].style.display = "block";
}

I managed to make it work by adding onLoad = "init()" property to the body tag. init() sets the image's display property to "block" when the page loads, as I understand, and that solved the issue but I want to understand why the display property didnt have a value initially.

Please note that I set the display property to "block" manually in css, although I don't think this should matter since img has a default display: inline, so a[0].style.display should return "inline" instead of an empty string.

This was my first question here so if there's something I should've done differently, please feel free to let me know :)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

From what I've read, an empty string can mean that its the element's default. So for div that would be block.

What does style.display = '' actually do?

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda